package com.example.web.merchant.hander;

import com.example.web.merchant.MerchantRequestParams;
import com.example.web.merchant.pojo.Merchant;
import com.example.web.merchant.service.impl.MerchantServicelmpl;
import com.example.web.merchant.service.impl.MerchatService;
import com.example.web.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("merchant")
public class MerchantHandler {

    @Autowired
    private MerchantServicelmpl merchantServicelmpl;

    //@GetMapping("qq")
    //public String xx() {
    //    return "xxxxxxxxx";
    //}

    @Resource
    private MerchatService merchatService;


    @PostMapping("add")
    public String add (@RequestBody MerchantRequestParams merchant) {
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        Merchant merchant_val = merchatService.findMerchant(merchant.getMname());
        //if (merchant_val != null) {
        //    return R.foil("该账号已经被别人注册");
        //}
        return merchant_val == null? String.valueOf(R.ok(merchatService.add(merchant))) : String.valueOf(R.fail("该账号已经被别人注册"));
    }
    @PostMapping("login")
    public R login(@RequestBody MerchantRequestParams merchant, HttpServletRequest request) {
        // 这里能够接收到前端商户提交过来的登录信息
        Merchant merchant_val = merchantServicelmpl.login(merchant);
        System.out.println("============================="+merchant);
        if (merchant_val != null) {
            request.getSession().setAttribute("mname", merchant.getMname());
        }
        return merchant_val != null ? R.ok("登录成功") : R.fail("账号或密码错误.");
    }
    @PostMapping("update")
    public R update(@RequestBody Merchant merchant, HttpServletRequest request) {
        //System.out.println("xxxxxxxxxxxxxxxx");
        merchant.setMname(request.getSession().getAttribute("mname").toString());
        System.out.println("用户信息：" + merchant);
        return merchatService.update(merchant) == 0 ? R.fail("修改失败") : R.ok("修改成功");
     }
     @GetMapping("all")
     public R all() {
        return merchatService.list() == null ? R.fail("获取失败") : R.ok(merchatService.list());
     }

     @GetMapping  ("status/{mid}")
     public R status(@PathVariable("mid") Integer mid,HttpServletRequest request) {
        String mname = request.getSession().getAttribute("mname").toString();
         System.out.println(">>>>>>>>>>>>>>>>>"+mname);
         if(!mname.equals("admin")){
            return R.fail("权限不足");
         }
        return merchatService.updateStatusBymId(mid) == 1 ? R.ok("审核成功"):R.fail("审核失败");
     }

    @GetMapping("unstatus/{mid}")
    public R unstatus(@PathVariable("mid") Integer mid,HttpServletRequest request) {
        String mname = request.getSession().getAttribute("mname").toString();
        System.out.println("----------------------------------------------"+mname);
        if(!mname.equals("admin")){
            return R.fail("权限不足");
        }
        return merchatService.updateStatusByMid(mid) == 1 ? R.ok("审核成功"):R.fail("审核失败") ;
    }

    @GetMapping("search")
    public R search(@RequestParam("mrealname") String mrealname) {
        return merchatService.findByCondition(mrealname) == null ? R.fail("没有找到该商户") : R.ok(merchatService.findByCondition(mrealname));
    }
}
